RV32G のクロスコンパイル環境を構築(Docker編)
 --with-arch=rv32g を渡すことで圧縮命令なしの RV32G の環境を構築した。
構築手順
rv32g ディレクトリを作成し,
code:sh
cd ~/src
mkdir rv32
Dockerfileを配置
code:~/rv32/Dockerfile
FROM ubuntu:18.04
LABEL version="1.0"
LABEL description="RV32G Compiler Environment"
WORKDIR /tmp/
RUN apt -y update
RUN apt -y install git autoconf automake autotools-dev curl python3 libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev
ENV RV32 /opt/rv32
RUN cd riscv-gnu-toolchain && \
mkdir build && cd build && \
../configure --prefix=$RV32 --with-arch=rv32g --with-abi=ilp32d && \
make -j$(nproc)
RUN echo 'export PATH=$PATH:$RV32/bin' >> ~/.bashrc
RUN apt install device-tree-compiler
RUN cd riscv-isa-sim && \
mkdir build && cd build && \
../configure --prefix=$RV32 --with-isa=rv32g && \
make && \
make install
RUN echo 'export PATH=$PATH:$RV32/bin' >> ~/.bashrc
RUN cd riscv-pk && \
git reset --hard 423801e35d048187d88fcbff55e20c4c34d27bee && \
mkdir build && cd build && \
export PATH=$PATH:$RV32/bin && \
../configure --prefix=$RV32/pk --host=riscv32-unknown-elf && \
make && \
make install
RUN echo 'export PATH=$PATH:$RV32/pk/riscv32-unknown-elf/bin' >> ~/.bashrc
WORKDIR /work
以下のコマンドでDockerイメージを作成(1時間くらいかかる...)
code:sh
cd ~/src/rv32
docker build -t ubuntu/rv32 .
Hello World
以下のコマンドでDockerコンテナの中に入り、
code:sh
docker run -it --rm -v $PWD:/work ubuntu/rv32
コンパイルして、
code:sh
riscv32-unknown-elf-gcc hello.c 
code:hello.c
int main() {
printf("Hello World!!\n");
printf("PI is %f\n", 3.1415);
}
実行する
code:sh
$ spike /opt/rv32/pk/riscv32-unknown-elf/bin/pk a.out 
bbl loader
Hello World!!
PI is 3.141500
16ビット圧縮命令が出力されていないことを確認
objdump で逆アセンブリして確認。命令がすべて32ビットっぽいので、ちゃんと RV32GC ではなく RV32G になっていそう。
code:sh
$ riscv32-unknown-elf-objdump -D a.out
a.out:     file format elf32-littleriscv
Disassembly of section .text:
00010094 <exit>:
10094:       ff010113                add     sp,sp,-16
10098:       00000593                li      a1,0
1009c:       00812423                sw      s0,8(sp)
100a0:       00112623                sw      ra,12(sp)
100a4:       00050413                mv      s0,a0
100a8:       028030ef                jal     130d0 <__call_exitprocs>
100ac:       1b81a503                lw      a0,440(gp) # 259c8 <_global_impur
e_ptr>
100b0:       03c52783                lw      a5,60(a0)
100b4:       00078463                beqz    a5,100bc <exit+0x28>
100b8:       000780e7                jalr    a5
100bc:       00040513                mv      a0,s0
100c0:       6550f0ef                jal     1ff14 <_exit>
000100c4 <register_fini>:
100c4:       00000793                li      a5,0
100c8:       00078863                beqz    a5,100d8 <register_fini+0x14>
100cc:       00014537                lui     a0,0x14
100d0:       8b450513                add     a0,a0,-1868 # 138b4 <__libc_fini_
array>
100d4:       11c0306f                j       131f0 <atexit>
100d8:       00008067                ret
000100dc <_start>:
100dc:       00015197                auipc   gp,0x15
100e0:       73418193                add     gp,gp,1844 # 25810 <__global_poin
ter$>
100e4:       1cc18513                add     a0,gp,460 # 259dc <__malloc_max_t
otal_mem>
100e8:       22818613                add     a2,gp,552 # 25a38 <__BSS_END__>
100ec:       40a60633                sub     a2,a2,a0
100f0:       00000593                li      a1,0
100f4:       188000ef                jal     1027c <memset>
100f8:       00003517                auipc   a0,0x3
100fc:       0f850513                add     a0,a0,248 # 131f0 <atexit>
10100:       00050863                beqz    a0,10110 <_start+0x34>
10104:       00003517                auipc   a0,0x3
10108:       7b050513                add     a0,a0,1968 # 138b4 <__libc_fini_a
rray>
1010c:       0e4030ef                jal     131f0 <atexit>
参考サイト